package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.githang.statusbar.StatusBarCompat

import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.OrderListAdapter
import com.ht.medicaltreatment.bean.OrderListBean
import com.ht.medicaltreatment.viewmodel.OrderListModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_order_list_layout.llAll
import kotlinx.android.synthetic.main.activity_order_list_layout.recyclerView
import kotlinx.android.synthetic.main.shift_to_layout.imgArrow
import kotlinx.android.synthetic.main.shift_to_layout.tvAll

class OrderListActivity : BaseActivity(), View.OnClickListener {
    private lateinit var adapter: OrderListAdapter
    private val model by viewModels<OrderListModel>()
private var isOpen = false

    override fun getLayoutId() = R.layout.activity_order_list_layout

    override fun init() {
        StatusBarCompat.setStatusBarColor(this,getColor(R.color.white),true)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = OrderListAdapter().apply { adapter = this }

        adapter.setOnItemClickListener { adapter, view, position ->
            val bean = adapter.getItem(position) as OrderListBean
            val intent = Intent(this,OrderDetailActivity::class.java)
            intent.putExtra("id",bean.id)
            startActivity(intent)
        }

        llAll.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        showProgress()
        if(isOpen){
            isOpen = false
            tvAll.text = "显示全部"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_bottom)
            model.getList(OrderListModel.OUT,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }else{
            isOpen = true
            tvAll.text = "收起"
            imgArrow.setBackgroundResource(R.drawable.icon_arrow_top)
            model.getAllList(OrderListModel.OUT,{
                adapter.setNewInstance(it)
                dismissProgress()
            },{
                dismissProgress()
                showToast(it)
            })
        }
    }

    override fun onResume() {
        super.onResume()
        showProgress()
        model.getList(OrderListModel.OUT,{
            adapter.setNewInstance(it)
            dismissProgress()
        }, {
            dismissProgress()
            ToastUtils.showShort(it)
        })
    }
}